package com.example.dirviesystem.Admin.controller;

import com.example.dirviesystem.Admin.pojo.AccountData;
import com.example.dirviesystem.Admin.pojo.NewPasswordData;
import com.example.dirviesystem.config.R;
import com.example.dirviesystem.entity.CoachInfo;
import com.example.dirviesystem.entity.CoachMessage;
import com.example.dirviesystem.entity.StudentInfo;
import com.example.dirviesystem.repository.CoachInfoRe;
import com.example.dirviesystem.repository.CoachMessageRe;
import com.example.dirviesystem.repository.StudentInfoRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
public class AccountController {
    @Autowired
    private StudentInfoRe studentInfoRepository;
    @Autowired
    private CoachInfoRe coachInfoRepository;

    @GetMapping("/getAllStudents")
    public List<StudentInfo> getAllStudents(){
        return studentInfoRepository.findAll();
    }
    @GetMapping("/getAllCoaches")
    public List<CoachInfo> getAllCoaches(){
        return coachInfoRepository.findAll();
    }

    @PostMapping("/createAccount")
    public R createAccount(@RequestBody AccountData accountData){
        System.out.println(accountData);
        if(accountData.getUserType().equals("student")){
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setName(accountData.getName());
            studentInfo.setApproved(false);
            studentInfo.setIdNumber(accountData.getIdNumber());
            studentInfo.setPassword(accountData.getPassword());
            studentInfo.setGender(accountData.getSex());
            studentInfoRepository.save(studentInfo);
        }
        else if(accountData.getUserType().equals("coach")){
            CoachInfo coachInfo = new CoachInfo();
            coachInfo.setName(accountData.getName());
            coachInfo.setQualified(false);
            coachInfo.setIdNumber(accountData.getIdNumber());
            coachInfo.setPassword(accountData.getPassword());
            coachInfo.setGender(accountData.getSex());
            coachInfoRepository.save(coachInfo);
        }
        return R.success("创建成功");
    }

    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody NewPasswordData newPasswordData){
        if (newPasswordData.getType().equals("student")){
            StudentInfo student = studentInfoRepository.findById(Long.valueOf(newPasswordData.getId()))
                    .orElseThrow(() -> new RuntimeException("学生不存在"));
            student.setPassword(newPasswordData.getNewPassword());
            studentInfoRepository.save(student);
        }
        else if (newPasswordData.getType().equals("coach")){
            CoachInfo coach = coachInfoRepository.findById(Long.valueOf(newPasswordData.getId()))
                    .orElseThrow(() -> new RuntimeException("教练不存在"));
            coach.setPassword(newPasswordData.getNewPassword());
            coachInfoRepository.save(coach);
        }
        return R.success("密码修改成功");
    }

    @DeleteMapping("/deleteCoachById/{id}")
    public R deleteCoachById(@PathVariable Long id) throws Exception{
        coachInfoRepository.deleteById(id);
        return R.success("删除成功");
    }

    @DeleteMapping("/deleteStudentById/{id}")
    public R deleteStudentById(@PathVariable Long id)throws Exception{
        studentInfoRepository.deleteById(id);
        return R.success("删除成功");
    }
}
